package wangm.service;

import com.alibaba.fastjson.JSONObject;
import wangm.bean.ExaminationPaper;
import wangm.bean.ExaminationPaperHistory;
import wangm.repository.ExaminationPaperHistoryRepository;
import wangm.util.StringsUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 历史试卷serviceImp
 *
 * @author zk
 * @date 2018-02-24 18:45
 */
@Service
public class ExaminationPaperHistoryServiceImp {
    private static Logger logger = LoggerFactory.getLogger(ExaminationPaperHistoryServiceImp.class);
    /**
     * 历史试卷repository
     */
    @Autowired
    private ExaminationPaperHistoryRepository paperHistoryRepository;

    /**
     * 保存已做过的试卷
     *
     * @param paperBean paperBean
     */
    public void savePaper(ExaminationPaper paperBean, String uid){
        //检查该人是否已经做过
        ExaminationPaperHistory paperHistoryBean = paperHistoryRepository.getByUidAndPid(uid,paperBean.getId());
        if(paperHistoryBean != null){
            return;
        }

        ExaminationPaperHistory saveBean = new ExaminationPaperHistory();
        saveBean.setId(StringsUtils.randomUUidString());
        saveBean.setPid(paperBean.getId());
        saveBean.setUid(uid);
        saveBean.setContent(JSONObject.toJSONString(paperBean));
        paperHistoryRepository.savePaperHistory(saveBean);
    }
}
